﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Acando.Dynamics.CRM.Silverlight.Library.Helper;
using System.ComponentModel;
using Acando.Dynamics.CRM.Silverlight.Library.Interfaces;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Acando.Dynamics.CRM.Silverlight.Library.CrmSdk;


namespace Acando.Dynamics.CRM.Silverlight.Library.Model
{
    public class DynamicDataModel : IDataModel
    {
        private string EntityName { get; set; }
        private CrmServiceSoapClient service;
        private String _organame;

        
        public DynamicDataModel(string entityname, String organame)
        {
            this.EntityName = entityname;
            this._organame = organame;
            this.service = SoapClientFactory.CreateCrmInstanceSilver();
            this.service.ExecuteCompleted += new EventHandler<ExecuteCompletedEventArgs>(ClientProxy_ExecuteCompleted);
        }

        #region Event-Handling

        void ClientProxy_ExecuteCompleted(object sender, ExecuteCompletedEventArgs e)
        {
            this.RetrieveMultipleCompleted(this, new RetrieveMultipleCompletedEventArgs(new object[] { ((RetrieveMultipleResponse)e.Result).BusinessEntityCollection }, e.Error, e.Cancelled, e.UserState));
        }

        #endregion
        

        #region IDataModel Members

        public void Create(BusinessEntity item)
        {
            WrapperServiceCall.CreateWrapper(this.service, item, this._organame);
            //this.service.CreateAsync(item);
        }

        public void Update(BusinessEntity item)
        {
            WrapperServiceCall.UpdateWrapper(this.service, item, this._organame);
            //this.service.UpdateAsync(item);
        }

        public void RetrieveMultiple()
        {
            RetrieveMultipleRequest req = new RetrieveMultipleRequest();
            req.ReturnDynamicEntities = true;
            req.Query = new QueryExpression() { EntityName = this.EntityName, ColumnSet = new AllColumns() };

            WrapperServiceCall.ExecuteWrapper(this.service, req, this._organame);
            //this.service.ExecuteAsync(req);
        }

        public void RetrieveMultiple(QueryBase query)
        {
            query.EntityName = this.EntityName;
            RetrieveMultipleRequest request = new RetrieveMultipleRequest();
            request.Query = query;
            request.ReturnDynamicEntities = true;

            WrapperServiceCall.ExecuteWrapper(this.service, request, this._organame);
            //this.service.ExecuteAsync(request);
        }


        public BusinessEntity NewItem()
        {
            return new DynamicEntity() { Name = this.EntityName, Properties = new Property[0] };
        }

        public BusinessEntity CopyItem(BusinessEntity item)
        {
            if (item == null)
                return null;
            else
                return SerializationHelper.DeserializeXmlTo<BusinessEntity>(SerializationHelper.SerializeToXml<BusinessEntity>(item));
        }

        public event EventHandler<RetrieveMultipleCompletedEventArgs> RetrieveMultipleCompleted;


        public event EventHandler<CreateCompletedEventArgs> Created
        {
            add { this.service.CreateCompleted += value; }
            remove { this.service.CreateCompleted -= value; }
        }

        public event EventHandler<AsyncCompletedEventArgs> Updated
        {
            add { this.service.UpdateCompleted += value; }
            remove { this.service.UpdateCompleted -= value; }
        }


        public event EventHandler<RetrieveCompletedEventArgs> RetrieveCompleted
        {
            add { this.service.RetrieveCompleted += value; }
            remove { this.service.RetrieveCompleted -= value; }
        }


        public string EntityTypeName
        {
            get { return this.EntityName; }
        }

        #endregion
    }
}
